package com.ray.ray.util;

import android.content.Context;
import android.net.Uri;

import com.google.android.mms.pdu.CharacterSets;
import com.google.android.mms.pdu.EncodedStringValue;
import com.google.android.mms.pdu.PduBody;
import com.google.android.mms.pdu.PduComposer;
import com.google.android.mms.pdu.PduPart;
import com.google.android.mms.pdu.SendReq;

public class MMSInfo {

	private Context context;
	private PduBody pduBody;
	private int partCount = 1;

	private String number;
	private String subject;
	private String text;
	private String imagePath;
	private String audioPath;

	public MMSInfo() {

	}

	public MMSInfo(Context context, String number, String subject, String text,
			String imagePath, String audioPath) {
		this.context = context;
		this.number = number;
		this.subject = subject;
		this.text = text;
		this.imagePath = imagePath;
		this.audioPath = audioPath;
		pduBody = new PduBody();
		addTextPart(text);
		addPart(imagePath);
	}

	/**
	 * 添加图片附件，每添加一个附件调用本方法一次即可
	 * 
	 * @author
	 * @param uriStr
	 *            , 如：file://mnt/sdcard//1.jpg
	 */
	public void addPart(String uriStr) {
		PduPart part = new PduPart();
		part.setCharset(CharacterSets.UTF_8);
		part.setName(("附件" + partCount++).getBytes());
		// part.setContentType(("image/jpg" +
		// getTypeFromUri(uriStr)).getBytes());// "image/png"
		part.setContentType("image/jpg".getBytes());
//		part.setDataUri(Uri.parse("file:///data/debug_6.jpg"));
		part.setDataUri(Uri.parse("file://"+uriStr));
		pduBody.addPart(part);
	}

	/**
	 * 添加文本信息
	 * 
	 * @param contenttext
	 */
	public void addTextPart(String contenttext) {
		PduPart part = new PduPart();
		part.setCharset(CharacterSets.UTF_8);
		part.setName(("附件" + partCount++).getBytes());
		// part.setContentType(("image/jpg" +
		// getTypeFromUri(uriStr)).getBytes());// "image/png"
		part.setContentType("text/plain".getBytes());
		part.setData(contenttext.getBytes());
		pduBody.addPart(part);
	}

	/**
	 * 将彩信的内容以及主题等信息转化成byte数组，准备通过http协议发送到"http://mmsc.monternet.com"
	 * 
	 * @author 邓
	 * @return
	 */
	public byte[] getMMSBytes() {
		PduComposer composer = new PduComposer(context, initSendReq());
		return composer.make();
	}

	/**
	 * 初始化SendReq
	 * 
	 * @author
	 * @return
	 */
	private SendReq initSendReq() {
		SendReq req = new SendReq();
		EncodedStringValue[] sub = EncodedStringValue.extract(subject);
		if (sub != null && sub.length > 0) {
			req.setSubject(sub[0]);// 设置主题
		}
		EncodedStringValue[] rec = EncodedStringValue.extract(number);
		if (rec != null && rec.length > 0) {
			req.addTo(rec[0]);// 设置接收者
		}
		req.setBody(pduBody);
		return req;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getImagePath() {
		return imagePath;
	}

	public void setImagePath(String imagePath) {
		this.imagePath = imagePath;
	}

	public String getAudioPath() {
		return audioPath;
	}

	public void setAudioPath(String audioPath) {
		this.audioPath = audioPath;
	}

}
